package com.cloudspace.jindun.window;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.utils.Convert;

public class MyPopupMenu extends PopupWindow implements View.OnClickListener, PopupWindow.OnDismissListener {
    private View.OnClickListener item1Listener;
    private View.OnClickListener item2Listener;
    private TextView item1Tv;
    private TextView item2Tv;

    private View secondItem;
    private LinearLayout menuly;

    public MyPopupMenu(Context context) {
        // 设置SelectPicPopupWindow的View
        View contentView = View.inflate(context, R.layout.menu_mychannel, null);
        setContentView(contentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        contentView.measure(0,0);
        setWidth(contentView.getMeasuredWidth());
        // 设置SelectPicPopupWindow弹出窗体的高
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        setFocusable(true);
        setOutsideTouchable(true);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        BitmapDrawable dw = new BitmapDrawable();
        setBackgroundDrawable(dw);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        setAnimationStyle(R.style.AnimTools);

        item1Tv = (TextView) contentView.findViewById(R.id.op_1);
        item2Tv = (TextView) contentView.findViewById(R.id.op_2);

        item1Tv.setOnClickListener(this);
        item2Tv.setOnClickListener(this);

        secondItem = contentView.findViewById(R.id.secondLy);
        menuly = (LinearLayout) contentView.findViewById(R.id.menuly);

        setOnDismissListener(this);
    }


    /**
     * 显示popupWindow
     *
     * @param parent anchor
     *
     * @return true: show  ,  false: hide
     */
    public boolean showOrHideOverflow(View parent,
                                      String text1,
                                      String text2,
                                      View.OnClickListener listener1,
                                      View.OnClickListener listener2
                                      ) {
        boolean showing = isShowing();
        if (!showing) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent,  0, 18);
            item1Listener = listener1;
            item2Listener = listener2;

            item1Tv.setText(text1);
            item2Tv.setText(text2);

            if (TextUtils.isEmpty(text2)){
                secondItem.setVisibility(View.GONE);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        Convert.dip2px(JindunApp.getInstance(), 50));
                menuly.setLayoutParams(params);
            }
        } else {
            this.dismiss();
        }
        return !showing;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.op_1:
                if (item1Listener != null)
                    item1Listener.onClick(v);
                break;
            case R.id.op_2:
                if (item2Listener != null)
                    item2Listener.onClick(v);
                break;
           default:
                break;
        }
        this.dismiss();
    }

    @Override
    public void onDismiss() {

    }
}
